<?php
class Model_Media extends Df_Model
{

    private $user_name;

    public function __construct ($user_name = null) {
        $this->user_name = $user_name;
    
     //        parent::__construct(new Model_DbTable_Action(), $id);
    }

    public function createFolder ($media_type) {
        //TODO remove it
        mkdir("media_data" . DIRECTORY_SEPARATOR . $this->user_name,0755);
        
        $album_name = "media_data" . DIRECTORY_SEPARATOR . $this->user_name .
         DIRECTORY_SEPARATOR . $media_type;
        mkdir($album_name, 0755);
    }

    public function removeSong ($name, $media_type) {
        $file = 'media_data' . DIRECTORY_SEPARATOR . $this->user_name .
         DIRECTORY_SEPARATOR . $media_type . DIRECTORY_SEPARATOR . $name;
        unlink($file);
    }

    public function getDirList ($dir_name, $user_name) {
        $results = array();
        
        $album_id = 'media_data' . DIRECTORY_SEPARATOR . $user_name .
         DIRECTORY_SEPARATOR . $dir_name;
        
        $handler = opendir($album_id);
        
        while ($file = readdir($handler)) {
            
            // if file isn't this directory or its parent, add it to the results
            if ($file != "." && $file != "..") {
                $results[] = $file;
            }
        
        }
        closedir($handler);
        
        return $results;
    }

    public function filter ($name) {
        $first = array('і','ї','А', 'а', 'Б', 'б', 'В', 'в', 'Г', 'г', 'Д', 'д', 'Е', 
        'е', 'Ё', 'ё', 'Ж', 'ж', 'З', 'з', 'И', 'и', 'Й', 'й', 'К', 'к', 'Л', 
        'л', 'М', 'м', 'Н', 'н', 'О', 'о', 'П', 'п', 'Р', 'р', 'С', 'с', 'Т', 
        'т', 'У', 'у', 'Ф', 'ф', 'Х', 'х', 'Ц', 'ц', 'Ч', 'ч', 'Ш', 'ш', 'Щ', 
        'щ', 'Ъ', 'ъ', 'Ы', 'ы', 'Ь', 'ь', 'Э', 'Э', 'Ю', 'ю', 'Я', 'я');
        
        $second = array('i','i','A', 'a', 'B', 'b', 'V', 'v', 'G', 'g', 'D', 'd', 'E', 
        'e', 'Yo', 'yo', 'Zh', 'zh', 'Z', 'z', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 
        'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'R', 'r', 'S', 's', 'T', 
        't', 'U', 'u', 'F', 'f', 'H', 'h', 'Ts', 'ts', 'Ch', 'ch', 'Sh', 'sh', 
        'Shch', 'shch', '', 'Y', 'y', '', 'E', 'e', 'Yu', 'yu', 'Ya', 'ya');
        
        $name = str_replace($first, $second, $name);
        
        $name = preg_replace("/[^0-9a-z\-\.\s]/i", "", $name);
        return $name;
    }

}